<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'form/guardable_form.class.php');
require_once(TANGRA_MAIN_DIR.'web_site/web_page/just_headers_view.class.php');
require_once(TANGRA_MAIN_DIR.'form/form_ctrl.class.php');

require_once($WSC->get_site_inc_dir().'_sys/site_web_page_wed.class.php');


require_once(TANGRA_MAIN_DIR.'exceptions/tangra_module_exception.class.php');


abstract class Site_Page_W_FCtrl extends Site_Web_Page_WED {
	protected $form_ctrl;
	private $redir_on_success;


	function __construct($page_name, $redir_on_success_target_page) {
		parent::__construct($page_name);

		$this->set_redir_on_success($redir_on_success_target_page);
	}


	public function init() {
		parent::init();

		$this->init_views();

		$this->_create_form_ctrl();


		$this->init_events();
	}


	protected function init_views() {
		$v = $this->create_view();
		$this->add_view($v);
	}


	protected function init_events() {
		$submit_events = $this->form_ctrl->get_form_submit_events();
		foreach($submit_events as $event_submit) {
			$this->wed->add_wed_event_action_pair($event_submit, 'form_submit');
		}

		$event_add_new = $this->form_ctrl->get_add_new_event();
		$this->wed->add_wed_event_action_pair($event_add_new, 'add_new_event');

		$event_object_id = $this->form_ctrl->get_object_id_event();
		$this->wed->add_wed_event_action_pair($event_object_id, 'receiving_object_id');
//		printbr($event_add_new->get_capture());
	}


	protected function default_event() {
		throw new Tangra_Module_Exception('Expecting '.$this->form_ctrl->get_add_new_event()->get_capture().' or '.$this->form_ctrl->get_object_id_event()->get_capture().' parameters.');
	}


	protected function show_form() {
		$this->form_ctrl->prepare_for_show();
		$this->add_exports($this->form_ctrl->get_tple_exports());
		$this->on_show_form();
		$view = $this->get_view('default');



		return $view;
	}


	protected function form_submit($params) {
		$context = $this->get__context();
		$rez = $this->form_ctrl->process_submit($context);

		if ($rez == Form_Ctrl::SUBMIT_RESULT_OK ) {
			$this->on_submit_processed_ok();

			$redir_view = $this->_create_default_redir_view();
			return $redir_view;
		} else {
			$this->on_submit_processed_not_ok();
			return $this->show_form();
		}
	}


	protected function receiving_object_id($object_id) {
		tangra_if_not_int_throw_e($object_id);

		$this->on_received_id($object_id);

		if ($this->form_ctrl->process_object_id($object_id)) {
			$view = $this->show_form();
		} else {
			$view = $this->create_redirect_view('record_not_found.php');
		}

		return $view;
	}


	protected function add_new_event() {
		$this->on_add_new();

		$this->form_ctrl->process_add_new();
		return $this->show_form();
	}


	protected function _create_form_ctrl() {
		$form_ctrl = $this->create_form_ctrl();
		if ($form_ctrl instanceof Form_Ctrl ) {
			$this->form_ctrl = $form_ctrl;

			$form_view = $this->form_ctrl->get_form_view();
			$form_view->set_charset_cascade($this->_config->get_default_encoding());
		} else {
//			var_dump_pre($form_ctrl);
			throw new Tangra_Module_Exception('Object returned by create_form_ctrl is not instance of Form_Ctrl.');
		}
	}


	protected function _create_default_redir_view() {
		$redir_view = $this->create_default_redir_view();
		if (!($redir_view instanceof Web_Page_View)) {
			throw new Tangra_Module_Exception('create_redir_view() returned object that is not instance of Just_Headers_View.');
		}

		return $redir_view;
	}


	protected function create_default_redir_view() {
		$redir_view = $this->create_redirect_view($this->redir_on_success);

		return $redir_view;
	}


	protected function set_redir_on_success($redir_on_success) {
		$this->redir_on_success = $redir_on_success;
	}


	public function get_redir_on_success() {
		return $this->redir_on_success;
	}


	abstract protected function create_form_ctrl();


	/**
	 * Called just before shoing the form (adding new, edit, resubmit in case of errors in the form)
	 *
	 */
	protected function on_show_form() {

	}


	/**
	 * Called right after controller processed submit successfully (i.e. no errors in the form and
	 * on_good_submit returned true) and before redirecting to success page.
	 *
	 */
	protected function on_submit_processed_ok() {
	}


	/**
	 * Called if controller processed submit unsuccessfully (i.e. there are errors in the form or
	 * on_good_submit returned false). Called before show_form().
	 *
	 */
	protected function on_submit_processed_not_ok() {
	}


	/**
	 * Will be called in case of received object id (edit mode) and just before handling
	 * control to form controller (process)
	 *
	 * @param unknown_type $object_id
	 */
	protected function on_received_id($object_id) {

	}

	/**
 	 * Will be called in case of add new (add new mode) and just before handling
	 * control to form controller (process)
	 *
	 */
	protected function on_add_new() {

	}
}